*>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
* SET GLOBAL $PATHS
*>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>

global root 	= "INSERT CUSTOMIZED PATH"
global data 	= "$root/data"
global data_out = "$root/data/processed/HealthProcess"
global figures	= "$root/figsandtabs"
global tables	= "$root/figsandtabs"

*>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>


/* This dofile produces a number of different results -- datasets, tables and figures */

/* DATASETS */

* prcere7bmnl.dta	estimated transition probabilities by age and starting state

* prcere71.dta		estimated transition probabilities for starting state 1

* mcere7S.dta		estimated health expenditure by age and state

* mcere7A.dta		mean expenditure by state

/* TABLES */

* lamfreq.txt		Table A6: Health Risk Categories $\lambda$ by Age Group: Low-Deductible Sample (Appendix)

* lamtran.txt		Table A7: Health Risk Category Transitions: Low-Deductible Sample (Appendix)

* lamfreqb.txt		Table 2: Health Expenditures and Risk Categories λ by Age Group 



/* FIGURES */

* contourL.pdf		Figure A3: Mean Expenditure by \lambda^*_{t-v}

* contour2.pdf		Figure A3: Mean Expenditure by \lambda^*_{t-v}

* cvreimall.pdf		Figure A9: Performance of Alternative Risk Classifications: Different Samples

* cvreimallA.pdf	Figure 3

* cvreimallC.pdf	Figure A8: Performance of Alternative Risk Classifications: lags of classification. 

* cvreimallAB.pdf	Figure A7: Performance of Alternative Risk Classifications: Winsorized Expenditure.

* histlamstar2b.pdf	Figure 2

* premb.pdf			Figure A11b


scalar agewide=5 /* Scalar for size of age bins */

/* Graph settings */

set scheme s1mono
graph set window fontface "Garamond"

global size size(large)
global ls labsize(medlarge)
global legs medlarge


use "${data}/raw/PKV/MainPKVSample.dta", clear
set more off


/* Winsorized Expenditure */

cap drop expenditureT
gen expenditureT=expenditure
sum expenditure if anasamp==1, detail
replace expenditureT=r(p99) if expenditureT>r(p99)


/* Generate lambdas */

xtset ID_VPNR year

gen l1lam=L.lambdastar0
gen l2lam=L2.lambdastar0
gen l3lam=L3.lambdastar0

gen l12lam=l1lam*l2lam
gen l23lam=l2lam*l3lam

xtile lam1=l1lam if anasamp==1, nq(10)
xtile lam2=l2lam if anasamp==1, nq(10)
xtile lam3=l3lam if anasamp==1, nq(10)


mkspline l1ls 10 = l1lam if anasamp==1&l2lam~=., pctile

mkspline l2ls 10 = l2lam if anasamp==1&l1lam~=., pctile

regress expenditure c.(l1ls1-l1ls10)##c.(l2ls1-l2ls10)
predict expp if e(sample)

*mvrs, degree(1) orthog knots(.2 .4 .6 1 1.5 2 2.5 3 5 10): regress expenditure l1lam l2lam if anasamp==1


mvrs, degree(3): regress expenditure l1lam l2lam if anasamp==1

foreach var of varlist l1lam_0 l1lam_1 l1lam_2 l1lam_3 {
foreach varb of varlist l2lam_0 l2lam_1 l2lam_2 l2lam_3 {
gen `var'`varb'=`var'*`varb'
}
}


reg expenditure l1lam_0- l1lam_3l2lam_3 if anasamp==1
predict pexp if e(sample)

xtile l1q=l1lam if anasamp==1, nq(20)
xtile l2q=l2lam if anasamp==1, nq(20)

bysort l1q: egen l1m=mean(l1lam)
bysort l2q: egen l2m=mean(l2lam)

set matsize 10000

reg expenditure lam1##c.l1lam lam2##c.l2lam lam1##lam2##c.l12lam if anasamp==1
predict ere if e(sample)

reg expenditure lam1##c.l1lam if anasamp==1&l2lam~=.
predict ere0 if e(sample)

reg expenditure lam1##c.l1lam lam2##c.l2lam lam3##c.l3lam lam1##lam2##c.l12lam lam2##lam3##c.l23lam if anasamp==1
predict ere2 if e(sample)

/* Clustering based on lambda */

foreach lamcat of numlist 2/12 {
cluster kmeans l1lam if anasamp==1, k(`lamcat') generate(c0ere`lamcat')

bysort c0ere`lamcat': egen c0ere`lamcat'b=mean(l1lam)
egen c0ere`lamcat'bb=group(c0ere`lamcat'b) if c0ere`lamcat'~=.
replace c0ere`lamcat'=c0ere`lamcat'bb
drop c0ere`lamcat'bb c0ere`lamcat'b
}


/* Clustering based on lambda plus one lag */

foreach lamcat of numlist 2/12 {
cluster kmeans pexp if anasamp==1, k(`lamcat') generate(cere`lamcat')

bysort cere`lamcat': egen cere`lamcat'b=mean(ere)
egen cere`lamcat'bb=group(cere`lamcat'b) if cere`lamcat'~=.
replace cere`lamcat'=cere`lamcat'bb
drop cere`lamcat'bb cere`lamcat'b
}


/* Clustering based on lambda plus two lags */

foreach lamcat of numlist 2/12 {
cluster kmeans ere2 if anasamp==1, k(`lamcat') generate(c2ere`lamcat')

bysort c2ere`lamcat': egen c2ere`lamcat'b=mean(ere2)
egen c2ere`lamcat'bb=group(c2ere`lamcat'b) if c2ere`lamcat'~=.
replace c2ere`lamcat'=c2ere`lamcat'bb
drop c2ere`lamcat'bb c2ere`lamcat'b
}

/* FIGURE A4 */

preserve
keep if anasamp==1&l2lam~=.
collapse expenditure pexp expp ere l1m l2m (count) nobs=expenditure, by(l1q l2q) /* To produce heat map, collapse by 20x20 ventile bins */

twoway contour expp l2m l1m, xscale(log) yscale(log) ccut(1000 2000 3000 4000 5000 6000 7000 8000 9000 10000) xlab(1 2 3 4 5 6, $ls) ylab(1 2 3 4 5 6, $ls)  zlab(1000 2000 5000 10000 15000, $ls) ///
ytitle("{&lambda}{superscript:{&lowast}}{sub:t-1}",height(7) $size) xtitle("{&lambda}{superscript:{&lowast}}{sub:t}",height(7) $size)  ztitle("Expenditure",height(7) $size) 

graph export "${figures}\contourL.pdf", replace

twoway contour expenditure l2m l1m, xscale(log) yscale(log) ccut(1000 2000 3000 4000 5000 6000 7000 8000 9000 10000) xlab(1 2 3 4 5 6, $ls) ylab(1 2 3 4 5 6, $ls)   zlab(1000 2000 5000 10000 15000, $ls) ///
ytitle("{&lambda}{superscript:{&lowast}}{sub:t-1}",height(7) $size) xtitle("{&lambda}{superscript:{&lowast}}{sub:t}",height(7) $size)  ztitle("Expenditure",height(7) $size)

graph export "${figures}\contour2.pdf", replace

restore

/* Evaluating performance of classifications with R2 from regressions */

cap drop regres*

qui reg expenditure female##ibn.agegr ibn.ID_TAR i.year if l1lam~=.&anasamp==1
matrix regres=e(r2)

qui reg expenditure female##ibn.agegr ibn.ID_TAR i.year if l1lam~=.&l2lam~=.&anasamp==1
matrix regres=regres,e(r2)

qui reg expenditure female##ibn.agegr ibn.ID_TAR i.year if l1lam~=.&l2lam~=.&l3lam~=.&anasamp==1
matrix regres=regres,e(r2)

matrix regres=regres,regres[1,2],regres[1,3],regres[1,3]

foreach num of numlist 2/12 {

reg expenditure ibn.c0ere`num' female##ibn.agegr ibn.ID_TAR  i.year if l1lam~=.&anasamp==1
matrix rhegres=e(r2)

reg expenditure ibn.c0ere`num' female##ibn.agegr ibn.ID_TAR  i.year if l1lam~=.&l2lam~=.&anasamp==1
matrix rhegres=rhegres,e(r2)

reg expenditure ibn.c0ere`num' female##ibn.agegr ibn.ID_TAR  i.year if l1lam~=.&l2lam~=.&l3lam~=.&anasamp==1
matrix rhegres=rhegres,e(r2)

reg expenditure ibn.cere`num' female##ibn.agegr ibn.ID_TAR  i.year if l1lam~=.&l2lam~=.&anasamp==1
matrix rhegres=rhegres,e(r2)

reg expenditure ibn.cere`num' female##ibn.agegr ibn.ID_TAR  i.year if l1lam~=.&l2lam~=.&l3lam~=.&anasamp==1
matrix rhegres=rhegres,e(r2)

reg expenditure ibn.c2ere`num' female##ibn.agegr ibn.ID_TAR  i.year if l1lam~=.&l2lam~=.&l3lam~=.&anasamp==1
matrix rhegres=rhegres,e(r2)

matrix regres=regres\rhegres

}

svmat regres
cap drop spl
cap gen spl=_n
label variable spl "Number of classes"

/* FIGURE A9 */

twoway (connected regres1 regres2 regres3 regres4 regres5 regres6 spl if spl<12),  ytitle("R{superscript:2}", $size) xtitle("Categories (K)",height(7) $size) legend(ring(0) pos(5) size($legs) cols(1) order(1 "No lags (q=1, sample 1)" 2 "No lags (q=1, sample 2)" 3 "No lags (q=1, sample 3)"  4 "One lag (q=2, sample 2)" 5 "One lag (q=2, sample 3)" 6 "Two lags (q=3, sample 3)")) ylabel(, $ls)  xlabel(, $ls)

graph export "${figures}\cvreimall.pdf", replace

/* FIGURE 2 */

twoway (connected regres3 regres5 regres6 spl if spl<12) (scatter regres5 spl if spl==7, msymbol(diamond) msize(medlarge) mfcolor(white)),  ytitle("R{superscript:2}", $size) xtitle("Categories (K)",height(7) $size) legend(ring(0) pos(5) size($legs) rows(4) order(4 "Preferred specification" 1 "No lags (n=0)"  2 "One lag (n=1)" 3 "Two lags (n=2)")) ylabel(, $ls grid)  xlabel(1(1)11, $ls)

graph export "${figures}\cvreimallA.pdf", replace

qui reg expenditure female##ibn.agegr ibn.ID_TAR i.year if l1lam~=.&l2lam~=.&anasamp==1
matrix regrez=e(r2)

/* Alternative approach: lags of classes instead of classes based on lags */

xtset ID_VPNR year

foreach num of numlist 2/12 {

reg expenditure ibn.c0ere`num'##L.c0ere`num' female##ibn.agegr ibn.ID_TAR  i.year if l1lam~=.&anasamp==1
matrix regrez=regrez\e(r2)

matrix regres=regres\rhegres

}

sort spl
cap drop regrez
svmat regrez

/* 	FIGURE A8 */

twoway (connected regres4 regrez1 regres2 spl if spl<12),  ytitle("R{superscript:2}", $size) xtitle("Categories (K)",height(7) $size) legend(ring(0) pos(5) size($legs) rows(3) order(1 "One lag (q=2)" 2 "No lags (q=1), interacted"  3 "No lags (q=1), simple")) ylabel(, $ls)  xlabel(, $ls)

graph export "${figures}\cvreimallC.pdf", replace

/* Winsorized expenditure */

set more off
cap drop regres*

qui reg expenditureT female##ibn.agegr ibn.ID_TAR i.year if l1lam~=.&anasamp==1
matrix regres=e(r2)

qui reg expenditureT female##ibn.agegr ibn.ID_TAR i.year if l1lam~=.&l2lam~=.&anasamp==1
matrix regres=regres,e(r2)

qui reg expenditureT female##ibn.agegr ibn.ID_TAR i.year if l1lam~=.&l2lam~=.&l3lam~=.&anasamp==1
matrix regres=regres,e(r2)

matrix regres=regres,regres[1,2],regres[1,3],regres[1,3]

foreach num of numlist 2/12 {

reg expenditureT ibn.c0ere`num' female##ibn.agegr ibn.ID_TAR  i.year if l1lam~=.&anasamp==1
matrix rhegres=e(r2)

reg expenditureT ibn.c0ere`num' female##ibn.agegr ibn.ID_TAR  i.year if l1lam~=.&l2lam~=.&anasamp==1
matrix rhegres=rhegres,e(r2)

reg expenditureT ibn.c0ere`num' female##ibn.agegr ibn.ID_TAR  i.year if l1lam~=.&l2lam~=.&l3lam~=.&anasamp==1
matrix rhegres=rhegres,e(r2)

reg expenditureT ibn.cere`num' female##ibn.agegr ibn.ID_TAR  i.year if l1lam~=.&l2lam~=.&anasamp==1
matrix rhegres=rhegres,e(r2)

reg expenditureT ibn.cere`num' female##ibn.agegr ibn.ID_TAR  i.year if l1lam~=.&l2lam~=.&l3lam~=.&anasamp==1
matrix rhegres=rhegres,e(r2)

reg expenditureT ibn.c2ere`num' female##ibn.agegr ibn.ID_TAR  i.year if l1lam~=.&l2lam~=.&l3lam~=.&anasamp==1
matrix rhegres=rhegres,e(r2)

matrix regres=regres\rhegres

}

svmat regres
cap drop spl
cap gen spl=_n
label variable spl "Number of classes"


/* FIGURE A7 */

twoway (connected regres3 regres5 regres6 spl if spl<12),  ytitle("R{superscript:2}", $size) xtitle("Categories (K)",height(7) $size) legend(ring(0) pos(5) size($legs) rows(3) order(1 "No lags (q=1)"  2 "One lag (q=2)" 3 "Two lags (q=3)")) ylabel(, $ls)  xlabel(, $ls)

graph export "${figures}\cvreimallAB.pdf", replace


/* Generate dataset including high-deductible plans */


sort l1lam

foreach num of numlist 0/3 {
gen nl1lam_`num'=l1lam_`num'
}

/* Transitions are estimated based on the entire sample -- including all levels of deductibles. Therefore need to impute risk classes for those */

foreach num of numlist 1/20 {
foreach var of varlist nl1* {
replace `var'=`var'[_n+`num'] if `var'==.&anasamp==0&l1lam~=.&l2lam~=. 
replace `var'=`var'[_n-`num'] if `var'==.&anasamp==0&l1lam~=.&l2lam~=.
}
codebook nl1lam_1 if l1lam~=.&l2lam~=.
}

sort l2lam

foreach num of numlist 0/3 {
gen nl2lam_`num'=l2lam_`num'
}


foreach num of numlist 1/20 {
foreach var of varlist nl2* {
replace `var'=`var'[_n+`num'] if `var'==.&anasamp==0&l1lam~=.&l2lam~=.
replace `var'=`var'[_n-`num'] if `var'==.&anasamp==0&l1lam~=.&l2lam~=.
}
codebook nl2lam_1 if l1lam~=.&l2lam~=.
}


foreach var of varlist nl1lam_0 nl1lam_1 nl1lam_2 nl1lam_3 {
foreach varb of varlist nl2lam_0 nl2lam_1 nl2lam_2 nl2lam_3 {
gen n`var'`varb'=`var'*`varb'
}
}

/* Rerun regression for expendiute to get out of sample predictions */

reg expenditure nl1lam_0- nnl1lam_3nl2lam_3 if anasamp==1
predict npexp if firsty<2009&agegr~=.


gen cere7b=cere7

tab cere7b anasamp

foreach lam of numlist 1/7 {
su pexp if cere7==`lam'&anasamp==1
replace cere7b=`lam' if anasamp==0&npexp>=r(min)&npexp<=r(max)
tab cere7b anasamp
}


xtset ID_VPNR year

gen fcoN=F.cere7b /* fcoN is the next-period risk-class */
replace fcoN=8 if cere7b~=.&deceased==1 /* Includes also deceased */

cap drop if _fillin==1
cap drop _fillin

xi, prefix(laag) noomit  i.cere7b*i.agegr

fillin cere7b agegr

set matsize 10000


scalar hight=0
local highest=15

global incvar ""
local except ""
ds  laag*X*
local varlist `r(varlist)'
local varlist: list varlist - except
di "`varlist'"

scalar nit=e(ic)

set more off

while `highest' > 14.067 {
foreach var of varlist `varlist' {

qui mlogit fcoN i.cere7b i.agegr $incvar `var' if firsty<2009&agegr~=., nocons noomit iterate(100)
scalar nit=e(ic)
qui test `var'
scalar net=r(chi2)
di "`var' `=net' `=nit'" 
if net>hight&net<.&nit<100 {
scalar hight=net
local qualv  "`var'"
}
}

di "`qualv'"
local highest=hight
di "`highest'"
if `highest'>  14.067 {
global incvar $incvar `qualv'
local except "`qualv'"
local varlist: list varlist - except
scalar hight=0
}
}

cap drop if _fillin==1
cap drop _fillin


xi, prefix(laag) noomit i.cere7b*i.agegr

mlogit fcoN i.cere7b i.agegr $incvar if firsty<2009&agegr~=., nocons noomit

drop laag*

cap drop if _fillin==1
cap drop _fillin
fillin cere7b agegr

xi, prefix(laag) noomit i.cere7b*i.agegr

cap drop fl1 fl2 fl3 fl4 fl5 fl6 fl7 fl8

predict fl1 fl2 fl3 fl4 fl5 fl6 fl7 fl8, pr

preserve
gen insamp=e(sample)
collapse fl1-fl8 (count) insamp, by(cere7b agegr)
drop if cere7b==.|agegr==.
rename cere7b cere7
rename insamp npeople
forvalues fl=1/8 {
label variable fl`fl' "Pr({&lambda}{subscript:t+1})=`fl'"
}
label variable npeople "Number of clients in cell"
label variable cere7 "Lambda category"

save "${data_out}\prcere7bmnl.dta", replace
ren cere7 state

keep if cere7==1

save "${data_out}\prcere71.dta", replace

restore

/* Estimate expenditure-age profile */

xtset ID_VPNR year

cap drop if _fillin==1
cap drop _fillin
cap drop _merge

xi, prefix(latk) noomit i.cere7*i.tkatn
xi, prefix(laag) noomit i.cere7*i.agegr
xi, prefix(tkag) noomit i.tkatn*i.agegr

fillin cere7 tkatn agegr

scalar hight=0
local highest=5

drop *cerX*_1_*
drop *Xage*_3


qui ds  latk*X* laag*X* tkag*X*
local varlist `r(varlist)' 
global incvar ""


while `highest' > 2 {
foreach var of varlist `varlist' {
qui reg expenditure ib1.cere7 ibn.tkatn ib3.agegr $incvar `var' if anasamp==1, noomit vce(cluster ID_VPNR)

*qui reg expenditure ibn.cere7 ibn.tkatn ibn.agegr $incvar `var' if anasamp==1, nocons noomit
scalar net=abs(_b[`var']/_se[`var'])
if net>hight {
if net<. {
scalar hight=net
local qualv  "`var'"
}
}
}
di "`qualv'"
local highest=hight
di "`highest'"
if `highest'>2 {
global incvar $incvar `qualv'
local except "`qualv'"
local varlist: list varlist - except
scalar hight=0
}
}

reg expenditure ib1.cere7 ibn.tkatn ib3.agegr $incvar if anasamp==1, noomit  vce(cluster ID_VPNR)

drop latk* laag* tkag*

xi, prefix(latk) noomit i.cere7*i.tkatn
xi, prefix(laag) noomit i.cere7*i.agegr
xi, prefix(tkag) noomit i.tkatn*i.agegr

cap drop expm
predict expm

preserve
gen insamp=e(sample)
collapse expm (count) insamp, by(cere7 tkatn agegr)
drop if tkatn==.|cere7==.|tkatn==.|agegr==.
gen idv=10*agegr+cere7
reshape wide expm insamp, i(idv) j(tkatn)
foreach var of  numlist 1/3 {
egen tkat`var'=total(insamp`var')
}
egen ttkat=rowtotal(tkat*)
egen npeople=rowtotal(insamp*)
foreach var of  numlist 1/3 {
replace tkat`var'=tkat`var'/ttkat
}
gen expm=(tkat1*expm1*(expm1>0)+tkat2*expm2*(expm2>0)+tkat3*expm3*(expm3>0))/(tkat1*(expm1>0)+tkat2*(expm2>0)+tkat3*(expm3>0))
drop tkat* insamp* expm1 expm2 expm3 idv ttkat
label variable expm "Mean Expenditure"
label variable npeople "Number of clients in cell"
label variable cere7 "Lambda category"
save "${data_out}\mcere7S.dta", replace

restore

xtset ID_VPNR year

cap drop if _fillin==1
cap drop _fillin
cap drop _merge

xi i.tkatn

regress expenditure i.cere7 _Itkatn_2 _Itkatn_3 if anasamp==1

su  _Itkatn_2
replace  _Itkatn_2=r(mean)
su _Itkatn_3
replace  _Itkatn_3=r(mean)

predict mexp if e(sample)

preserve
gen insamp=e(sample)
collapse mexp (count) insamp, by(cere7)
drop if cere7==.
rename mexp expm
rename insamp npeople
label variable expm "Mean Expenditure"
label variable npeople "Number of clients in cell"
label variable cere7 "Lambda category"
save "${data_out}\mcere7A.dta", replace
restore



/* Transitions in low-deductible sample */

tab agegr cere7 if anasamp==1, row nofreq matcell(lamfreq)

matrix list lamfreq

mata : freqs = st_matrix("lamfreq")
mata : freqs :/ rowsum(freqs)

mata: st_matrix("lamfreq2", freqs :/ rowsum(freqs))

matrix rownames lamfreq2 = $rown2

matrix rownames lamfreq2 = "25-30" "30-35" "35-40" "40-45" "45-50" "50-55" "55-60" "60-65" "65-70" "70-75" "75+"

estout matrix(lamfreq2, fmt(3)) using "${tables}\lamfreq.txt", style(tex) replace ///
mlabels(,none) collabels(,none) eqlabels(,none) 

xtset ID_VPNR year

tab cere7 fcoN if anasamp==1, row nofreq matcell(lamfreq)

matrix list lamfreq

mata : freqs = st_matrix("lamfreq")
mata : freqs :/ rowsum(freqs)

mata: st_matrix("lamfreq2", freqs :/ rowsum(freqs))

matrix rownames lamfreq2 = 1 2 3 4 5 6 7


estout matrix(lamfreq2, fmt(3)) using "${tables}\lamtran.txt", style(tex) replace ///
mlabels(,none) collabels(,none) eqlabels(,none) 

xtile mpct=expenditure if anasamp==1, nq(100)

twoway (histogram lambdastar0 if lambdastar0<10&year==2006&firsty<2009&agegr~=., start(0) width(0.1) color(gs10)) (histogram lambdastar0 if lambdastar0<10&year==2011& firsty<2009&agegr~=., start(0) width(0.1)  fcolor(none) lcolor(black)),   xtitle("ACG Score ({&lambda}{superscript:{&lowast}}{sub:t})", $size height(7)) ytitle(, $size) xlabel(, format(%9.0g) $ls) ylabel(, $ls) legend(ring(0) pos(1) size($legs) rows(2) order(1 "2006 Distribution" 2 "2011 Distribution" ))

gen lambdag10=(lambdastar0>10) if lambdastar0<.

su lambdag10 if firsty<2009&agegr~=.

graph export "${figures}\histlamstar2b.pdf",  replace

levelsof agegr, local(ageg)

tab agegr cere7b if  firsty<2009&agegr~=., row nofreq matcell(lamfreq)

matrix list lamfreq

mata : freqs = st_matrix("lamfreq")
mata : freqs :/ rowsum(freqs)

mata: st_matrix("lamfreq2", freqs :/ rowsum(freqs))

matrix sust=0,0

foreach agr of local ageg {
bysort cere7: egen mrem=mean(expenditure) if agegr==`agr'&l1lam~=.&anasamp==1

su mrem if agegr==`agr'&l1lam~=.&anasamp==1
matrix sust=sust\(r(mean),r(sd))
drop mrem

}

matrix sust=sust[2...,1...]

matrix list sust

matrix lamfreq2=sust,lamfreq2

matrix list lamfreq2


/* Table 2 */

matrix rownames lamfreq2 = "25-30" "30-35" "35-40" "40-45" "45-50" "50-55" "55-60" "60-65" "65-70" "70-75" "75+"

estout matrix(lamfreq2, fmt(%9.0fc %9.0fc  3 3 3 3 3 3 3)) using "${tables}\lamfreqb.txt", style(tex) replace ///
mlabels(,none) collabels(,none) eqlabels(,none) 


preserve

cap drop fl1- fl8

merge m:1 agegr using "${data_out}\prcere71"


keep if firsty<2009&agegr~=.

gen deduc=deductible*(1+civserv) /* Need to take into account that civil servants get half their claims covered */

drop _merge

gen yearplg=floor(bjgpers/5)
replace yearplg=-1 if bjgpers<2

xi i.tkatn, noomit

reg prem (_Itkatn_1 _Itkatn_2)##agegr##c.deduc if yearplg==1
predict resids if e(sample), residuals
su resids, detail

su _Itkatn_1
replace  _Itkatn_1=r(mean)

su _Itkatn_2
replace  _Itkatn_2=r(mean)

replace deduc=0

predict premb if e(sample)
gen premc=premb+resids

bysort agegr yearplg (premc): gen pqa=(_n-1)/(_N-1)

gen premcat=1 if pqa<=fl1
replace premcat=2 if pqa>fl1&pqa<=fl1+fl2
replace premcat=3 if pqa>fl1+fl2

/* Figure A11b */

binscatter premc agegr if yearplg==1&agegr<10, by(premcat) linetype(connect) ///
 legend(ring(0) pos(11) cols(1)  size($legs) order(3 "{&lambda}>2" 2 "{&lambda}=2" 1 "{&lambda}=1")) ///
 ytitle("Annual Premium  (P{sub:t}({&xi}{sub:t}); USD)", $size)  xtitle("Age at Inception", $size) yscale(r(0 15000)) ///
 ylabel(0(5000)20000, $ls format( %12.0fc)) xlabel(0 "25-30" 2 "35-40" 4 "45-50" 6 "55-60" 8 "65-70", $ls) ///
 colors(black gs7 gs12)

graph export "${figures}\premb.pdf", replace

restore

** export relevant files to CSV

u "${data_out}\mcere7A.dta", clear
ren cere7 state
ren exmp mu
export delimited using "${data_out}\mcere7A.csv"

u "${data_out}\prcere7bmnl.dta", clear
ren cere7 state
drop npeople
export delimited using "${data_out}\prcere7bmnl.csv"





